package com.photoeditor.demo.ui.filter;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.Spannable;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;


import com.common.base.ComponentContext;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.photoeditor.demo.db.SubtitleAnimBean2;
import com.photoeditor.demo.util.ScreenUtils;
import com.photoeditor.demo.videofilter.com.daasuu.epf.Resolution;
import com.photoeditor.demo.videofilter.com.daasuu.epf.filter.GlOverlayFilter;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by zhangwei on 19-6-25.
 * 真正进行视频文字的绘制动画那些的控制
 */
public class VideoSubtitleFilter extends GlOverlayFilter {

    private SimpleExoPlayer mPlayer;
    private List<SubtitleAnimBean2> beans;

    private TextPaint mPaint;
    private boolean drawStopped;

    public VideoSubtitleFilter(SimpleExoPlayer player) {
        this.mPlayer = player;
        int width = ScreenUtils.getScreenWidth(ComponentContext.getContext());
        int height = (int) (width * 1.0112f);
        inputResolution = new Resolution(width, height);
        beans = new CopyOnWriteArrayList<>();
        initPaint();
    }

//    public VideoSubtitleFilter(SubtitleAnimBean bean) {
//        super();
//        this.bean = bean;
//        int width = ScreenUtils.getScreenWidth(ComponentContext.getContext());
//        int height = (int) (width * 1.0112f);
//        inputResolution = new Resolution(width, height);
//        initPaint();
//    }

    public void setBeans(List<SubtitleAnimBean2> newBeans) {
        if (beans == null) {
            return;
        }

        beans.clear();
        if (newBeans != null) {
            for (SubtitleAnimBean2 bean : newBeans) {
                beans.add(bean);
            }
        }
    }

    public void removeBean(SubtitleAnimBean2 bean) {
        if (bean == null) {
            return;
        }

        beans.remove(bean);
    }

    public void stopDraw() {
        drawStopped = true;
    }

    public void startDraw() {
        drawStopped = false;
    }

    public int getBeanSize() {
        return beans.size();
    }

    private void initPaint() {
        mPaint = new TextPaint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextAlign(Paint.Align.LEFT);
        Typeface currentTypeFace = mPaint.getTypeface();
        Typeface bold = Typeface.create(currentTypeFace, Typeface.BOLD);
        mPaint.setTypeface(bold);
    }

    @Override
    protected void drawCanvas(Canvas canvas) {
//        if (drawStopped || beans.isEmpty() || mPlayer == null) {
        if (beans.isEmpty() || mPlayer == null) {
            return;
        }

//        Log.e("playerTimer", "duration: " + mPlayer.getDuration() + ", position: " + mPlayer.getCurrentPosition());
        for (SubtitleAnimBean2 bean : beans) {
            long duration = mPlayer.getDuration();
            if(duration<=0){
                continue;
            }
            long position  = mPlayer.getCurrentPosition();
            if (position < bean.getStartPositionMs()) { // 未到动画开始时间
                continue;
            }
            if (position > bean.getEndPositionMs()) {  // 超过动画结束时间
//                removeBean(bean);
                continue;
            }

            PointF pointF = bean.getCurrentPosition(position, duration);
            if (pointF == null) {
                return;
            }

            float scale = bean.getCurrentScale(position, duration);
            mPaint.setTextSize(bean.getTextSize() * scale);
            mPaint.setColor(bean.getTextColor());
            mPaint.setTypeface(bean.getTextFont());
            mPaint.setAlpha((int) (bean.getCurrentAlpha(position, duration) * 255));

            CharSequence text = bean.getText();
            boolean isAnim = bean.isAnim();
            int maxWidth = bean.getMaxWidth();
            Layout.Alignment alignment = bean.getAlignment();
            StaticLayout textLayout = new StaticLayout(text, mPaint, maxWidth > 0 ? maxWidth : canvas.getWidth(),
                    alignment, 1.0f, 0.0f, false);
            canvas.save();

            if (!isAnim) {
                canvas.translate(pointF.x, pointF.y);
            } else if (scale == 1f) {
                canvas.translate(pointF.x, pointF.y);
            } else {
                TextPaint paint = new TextPaint(mPaint);
                paint.setTextSize(bean.getTextSize());
                StaticLayout tempTextLayout = new StaticLayout(text, paint, canvas.getWidth(), alignment, 1.0f, 0.0f, false);
                float originalHeight = tempTextLayout.getHeight();
                float originalLineWidth = tempTextLayout.getLineWidth(0);
                int lineCount = tempTextLayout.getLineCount();
                for (int i=1; i<lineCount; i++) {
                    float lineWidth = tempTextLayout.getLineWidth(i);
                    originalLineWidth = originalLineWidth >= lineWidth ? originalLineWidth : lineWidth;
                }

                float currentHeight = textLayout.getHeight();
                float currentLineWidth = textLayout.getLineWidth(0);
                for (int i=1; i<lineCount; i++) {
                    float lineWidth = textLayout.getLineWidth(i);
                    currentLineWidth = currentLineWidth >= lineWidth ? currentLineWidth : lineWidth;
                }

                float dx = pointF.x + (originalLineWidth - currentLineWidth) / 2f;
                float dy = pointF.y + (originalHeight - currentHeight) / 2f;
                canvas.translate(dx, dy);
            }

            textLayout.draw(canvas);

            canvas.restore();

        }
    }

}
